package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.FacilitiesDao;
import cn.topevery.garden.entity.po.Facilities;
import cn.topevery.garden.entity.query.FacilitiesQuery;
import cn.topevery.garden.entity.vo.FacilitiesPageVO;
import cn.topevery.garden.entity.vo.FacilitiesPoiVO;
import cn.topevery.garden.entity.vo.oneMap.CountVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.db.enums.ScriptExistsConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author code-generate
 * @date 2021-08-06
 */
@Repository
public class FacilitiesDaoImpl extends BaseDaoImpl<Facilities> implements FacilitiesDao {
    @Override
    public <P extends DbCsp> List<Facilities> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<FacilitiesPageVO> listPage(FacilitiesQuery query) {
        String table = "t_facilities tf";

        String columns = "tf.*";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tf.c_id desc");
        sqlQueryMate
                .where(true, "tf.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getName()), "tf.c_name", ScriptConditionEnum.Eq, query.getName())
                .where(StringUtils.isNotBlank(query.getCode()), "tf.c_code", ScriptConditionEnum.Like, query.getCode())
                .where(StringUtils.isNotBlank(query.getParkName()), "tf.c_park_name", ScriptConditionEnum.Like, query.getParkName())
                .where(query.getTypeList() != null && !query.getTypeList().isEmpty(), "tf.c_type_id", ScriptConditionEnum.In, query.getTypeList())
                //.where(query.getCodeList() != null && !query.getCodeList().isEmpty(), "tf.c_name", ScriptConditionEnum.In, query.getCodeList())
                .where(StringUtils.isNotBlank(query.getTypeId()), "tf.c_type_id", ScriptConditionEnum.Eq, query.getTypeId())
                .where(query.getParkId() != null, "tf.c_park_id", ScriptConditionEnum.Eq, query.getParkId())
                .whereExists(query.getExtentId() != null, ScriptExistsConditionEnum.EXISTS, "select 1 From t_task_info tti join t_task_facilities_relation ttfr on tti.c_id=ttfr.c_task_id where tti.c_extent_id=:extentId and ttfr.c_facilities_id=tf.c_id", new KeyValue[]{new KeyValue("extentId", query.getExtentId())});


        return getDataPager(sqlQueryMate, pageSize, pageIndex, FacilitiesPageVO.class);
    }

    /**
     * 描述 获取统计数量
     *
     * @param name     the name
     * @param extentId the extent id
     * @return the count
     * @author wenzhao.lin
     * @created 2021 -08-25 09:24:05
     */
    @Override
    public Map getCount(String name, Integer extentId, String typeId) {
        String table = "t_facilities tp";

        String columns = "count(1) value,'设备设施数量' as name";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(name), "tp.c_name", ScriptConditionEnum.Like, name)
                .where(extentId != null, "tp.c_park_id", ScriptConditionEnum.Eq, extentId)
                .where(StringUtils.isNotBlank(typeId), "tp.c_type_id", ScriptConditionEnum.Eq, typeId)
        ;
        return getOneMap(sqlQueryMate);
    }

    /**
     * 描述 通过类型获取列表
     *
     * @param name     the name
     * @param extentId the extent id
     * @param type     the type
     * @return the person list by type
     * @author wenzhao.lin
     * @created 2021 -08-25 09:25:36
     */
    @Override
    public List<Facilities> getFacilitiesListByType(String name, Integer extentId, String type) {
        String table = "t_facilities tp";

        String columns = "tp.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(name), "tp.c_name", ScriptConditionEnum.Like, name)
                .where(extentId != null, "tp.c_extent_id", ScriptConditionEnum.Eq, extentId)
                .where(type != null, "tp.c_type_id", ScriptConditionEnum.Eq, type)
        ;
        List<Facilities> list = selectList(sqlQueryMate, Facilities.class);
        return list;
    }

    @Override
    public List<FacilitiesPoiVO> getFacilitiesPoiListByType(FacilitiesQuery query) {
        String table = "t_facilities tp";

        String columns = "tp.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getName()), "tp.c_name", ScriptConditionEnum.Like, query.getName())
                .where(query.getExtentId() != null, "tp.c_extent_id", ScriptConditionEnum.Eq, query.getExtentId())
                .where(query.getTypeId() != null, "tp.c_type_id", ScriptConditionEnum.Eq, query.getTypeId())
        ;
        List<FacilitiesPoiVO> list = selectList(sqlQueryMate, FacilitiesPoiVO.class);
        return list;
    }


    /**
     * 描述 获取类型列表
     *
     * @param name     the name
     * @param extentId the extent id
     * @return the person type list
     * @author wenzhao.lin
     * @created 2021 -08-27 09:15:52
     */
    @Override
    public List<Facilities> getTypeList(String name, Integer extentId) {
        String table = "t_facilities tp";

        String columns = "distinct tp.c_type_id type, tp.c_type_name ";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(name), "tp.c_name", ScriptConditionEnum.Like, name)
                .where(extentId != null, "tp.c_park_id", ScriptConditionEnum.Eq, extentId)
        ;

        List<Facilities> list = selectList(sqlQueryMate, Facilities.class);
        for (Facilities facilities : list) {
            if (StringUtils.isNotEmpty(facilities.getType())) {
                Map map = getCount(name, extentId, facilities.getType());
                facilities.setTotalNumber(String.valueOf(map.get("value")));
            }
        }
        return list;
    }

    @Override
    public Facilities getByCodeAndType(String code, Integer type) {

        String table = "t_facilities tf join t_device_object_relate tdor on  tdor.c_object_id = tf.c_id join t_gps_device tgd on tgd.c_id=tdor.c_device_id ";
        String columns = "tf.*";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tf.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tdor.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tgd.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tdor.c_object_type", ScriptConditionEnum.Eq, type)
                .where(true, "tgd.c_code", ScriptConditionEnum.Eq, code);
        AtomicReference<Facilities> vo = new AtomicReference<>(new Facilities());
        run(sqlQueryMate.getSql(), CommandType.Text, dbCommand -> {
            sqlQueryMate.getParameters().forEach(dbCommand::addParameter);
            vo.set(dbCommand.executeToEntity(Facilities.class));
        });
        return vo.get();
    }

    /**
     * 描述 一张图管理 - 获取设施统计数量
     *
     * @return the type count
     * @author wenzhao.lin
     * @created 2021 -09-07 16:54:23
     */
    @Override
    public List<CountVO> getTypeCount() {
        StringBuffer sb = new StringBuffer();
        sb.append("select count(1) value, c_type_name name from t_facilities where c_db_status=0 group by c_type_name order by value desc");

        AtomicReference<List<CountVO>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(CountVO.class));
        });

        return vo.get();
    }

    @Override
    public List<Map<String, Object>> facilitiesCount() {
        String sql = "SELECT c_type_id typeId, c_type_name typeName, count(*) count FROM `t_facilities` where c_db_status=0 GROUP BY c_type_id, c_type_name";
        return getMap(sql);
    }
}
